package com.hanthink.web.controller.label;

import com.hanthink.common.annotation.Log;
import com.hanthink.common.core.controller.BaseController;
import com.hanthink.common.core.domain.AjaxResult;
import com.hanthink.common.core.domain.model.LoginUser;
import com.hanthink.common.core.page.TableDataInfo;
import com.hanthink.common.enums.BusinessType;
import com.hanthink.common.utils.ServletUtils;
import com.hanthink.common.utils.poi.ExcelUtil;
import com.hanthink.framework.web.service.TokenService;
import com.hanthink.system.domain.label.LabelRecord;
import com.hanthink.system.domain.label.LabelVerification;
import com.hanthink.system.service.label.ILabelRecordService;
import com.hanthink.system.service.label.ILabelVerificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * labelController
 * 
 * @author Shuyu Li
 * @date 2023-02-07
 */
@RestController
@RequestMapping("/label/LabelRecord")
public class LabelRecordController extends BaseController
{
    @Autowired
    private ILabelRecordService labelRecordService;

    @Autowired
    private TokenService tokenService;
    /**
     * 查询-列表
     */
    @PreAuthorize("@ss.hasPermi('label:LabelRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(LabelRecord labelRecord)
    {
        startPage();
        List<LabelRecord> list = labelRecordService.selectLabelRecordList(labelRecord);
        return getDataTable(list);
    }

    /**
     * 导出标签列表
     */
    @PreAuthorize("@ss.hasPermi('label:LabelRecord:export')")
    @Log(title = "-", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LabelRecord labelRecord)
    {
        List<LabelRecord> list = labelRecordService.selectLabelRecordList(labelRecord);
        ExcelUtil<LabelRecord> util = new ExcelUtil<LabelRecord>(LabelRecord.class);
        return util.exportExcel(list, "LabelRecord");
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('label:LabelRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(labelRecordService.selectLabelRecordById(id));
    }

    /**
     * 新增标签
     */
    @PreAuthorize("@ss.hasPermi('label:LabelRecord:add')")
    @Log(title = "-", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LabelRecord labelRecord)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        return toAjax(labelRecordService.insertLabelRecord(labelRecord,operName));
    }

    /**
     * 修改标签
     */
    @PreAuthorize("@ss.hasPermi('label:LabelRecord:edit')")
    @Log(title = "-", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LabelRecord labelRecord)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        return toAjax(labelRecordService.updateLabelRecord(labelRecord,operName));
    }

    /**
     * 删除标签
     */
    @PreAuthorize("@ss.hasPermi('label:LabelRecord:remove')")
    @Log(title = "-", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        return toAjax(labelRecordService.deleteLabelRecordByIds(ids,loginUser.getUser().getUserName()));
    }

    /**
     * 下载模板
     */
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<LabelRecord> util = new ExcelUtil(LabelRecord.class);
        return util.importTemplateExcel("标签数据");
    }
    /**
     * 导入标签数据
     */
    @Log(title = "标签管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('label:LabelRecord:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<LabelRecord> util = new ExcelUtil(LabelRecord.class);
        List<LabelRecord> labelList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = labelRecordService.importPlan(labelList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 一键启用功能
     */
    @PreAuthorize("@ss.hasPermi('label:LabelRecord:change')")
    @Log(title = "一键启用", businessType = BusinessType.UPDATE)
    @PostMapping("/openStatusByIds")
    public AjaxResult changeStatusByIds(@RequestBody Long[] ids)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        return toAjax(labelRecordService.changeStatusByIds(ids,loginUser.getUser().getUserName()));
    }
}
